package com.netease.nie.facade;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.common.CommonBusinessTools;
import com.netease.nie.constants.CommonRedisKeyConstatns;
import com.netease.nie.dto.CategoryDto;
import com.netease.nie.service.CategoryService;

/**
 * 
 * @author KingsHunter
 * @createDate February 8th,2017
 *
 */
@Service
public class CategoryFacadeImpl extends CommonBusinessTools implements
		CategoryFacade {

	private static final String CATEGORY_LIST_KEY = CommonRedisKeyConstatns.CATEGORY_LIST_KEY;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private Redis redis;

	public List<CategoryDto> listAllCategoryDtos() {
		boolean exists = redis.exists(CATEGORY_LIST_KEY);
		List<CategoryDto> categoryDtos = null;
		if (!exists) {
			categoryDtos = categoryService.listAllCategoryDtos();
			if (CollectionUtils.isNotEmpty(categoryDtos))
				redis.rpush(CATEGORY_LIST_KEY, categoryDtos);
		} else
			categoryDtos = redis.lrange(CATEGORY_LIST_KEY, 0, -1, CategoryDto.class);
		return categoryDtos;
	}
	
}
